// +---------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +---------------------------------------------------------------------
// | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
// +---------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +---------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +---------------------------------------------------------------------

import {
	getUserInfo
} from "../../api/user.js";
import {
	LOGIN_STATUS,
	UID
} from '../../config/cache';
import Cache from '../../utils/cache';
import {
	USER_INFO
} from '../../config/cache';
const state = {
	token: Cache.get(LOGIN_STATUS) || false,
	backgroundColor: "#fff",
	userInfo: Cache.get(USER_INFO) || {},
	uid: Cache.get(UID) || 0,
	// 是否登录
	isLogin: Cache.get(LOGIN_STATUS) ? true : false,
	homeActive: false,
	phoneStatus:true,
	
};

const mutations = {
	// 记录是否登录
	SETISLOGIN(state,val){
		state.isLogin = val;
	},
	SETPHONESTATUS(state,val){
		state.phoneStatus = val;
	},
	// 登录保存token
	LOGIN(state, opt) {
		state.token = opt.token;
		Cache.set(LOGIN_STATUS, opt.token, opt.time);
	},
	
	// 存储uid
	SETUID(state,val){
		state.uid = val;
		Cache.set(UID, val);
	},
	// 修改token
	UPDATE_LOGIN(state, token) {
		state.token = token;
	},
	// 删除token，uid
	LOGOUT(state) {
		state.token = undefined;
		state.uid = undefined
		Cache.clear(LOGIN_STATUS);
		Cache.clear(UID);
	},
	// 背景颜色
	BACKGROUND_COLOR(state, color) {
		state.color = color;
		document.body.style.backgroundColor = color;
	},
	
	// 储存用户信息
	UPDATE_USERINFO(state, userInfo) {
		state.userInfo = userInfo;
		Cache.set(USER_INFO, userInfo);
	},
	// 裸眼模式
	OPEN_HOME(state) {
		state.homeActive = true;
	},
	CLOSE_HOME(state) {
		state.homeActive = false;
	},
};

// const actions = {
// 	USERINFO({
// 		state,
// 		commit
// 	}, force) {
// 		if (state.userInfo !== null && !force)
// 			return Promise.resolve(state.userInfo);
// 		else
// 			return new Promise(reslove => {
// 				getUserInfo().then(res => {
// 					commit("UPDATE_USERINFO", res.data);
// 					Cache.set(USER_INFO, res.data);
// 					reslove(res.data);
// 				});
// 			}).catch(() => {

// 			});
// 	}
// };

export default {
	state,
	mutations
};
